package com.xcdh.trading.service;

import com.xcdh.trading.util.TweetParser;
import lombok.SneakyThrows;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;

@Service
public class MskTweetService {
    private final static String token = "AAAAAAAAAAAAAAAAAAAAALrgzQEAAAAAb1bt9ca7MZLJVo9gIe%2BrcCk1ghI%3DakL8odD54LsUq9QFWSUN3Yeya6cTTGVN7m2PAbEzaswgGDiLXf" ;
    private final static  String msk_user_id = "44196397"; // 马斯克的用户 ID

    private final RestTemplate restTemplate ;

    @SneakyThrows
    public MskTweetService(){
        // 创建一个信任所有证书的 SSLContext
        SSLContext sslContext = new SSLContextBuilder()
                .loadTrustMaterial(null, TrustAllStrategy.INSTANCE)
                .build();
        // 创建一个忽略主机名验证的 SSLConnectionSocketFactory
        SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
                sslContext,
                NoopHostnameVerifier.INSTANCE
        );

        // 配置代理
        HttpHost proxy = new HttpHost("127.0.0.1", 7890);

        // 创建 HTTP 客户端
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(sslConnectionSocketFactory)
                .setProxy(proxy)
                .setDefaultRequestConfig(RequestConfig.custom()
                        .setConnectTimeout(5000)
                        .setSocketTimeout(5000)
                        .build())
                .build();

        // 创建请求工厂
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        restTemplate = new RestTemplate(requestFactory) ;
    }

    public void syncMskMskTweet(){
        String apiUrl = "https://api.twitter.com/2/users/" + msk_user_id + "/tweets?max_results=50&tweet.fields=created_at";

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + token);
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 创建请求实体
        HttpEntity<String> entity = new HttpEntity<>(headers);

        try {
            // 发送 GET 请求
            ResponseEntity<String> response = restTemplate.exchange(apiUrl, HttpMethod.GET, entity, String.class);

            // 处理响应
            if (response.getStatusCode() == HttpStatus.OK) {
                String responseBody = response.getBody();
                TweetParser.parseTweets(responseBody);
            } else {
                System.out.println("请求失败，状态码：" + response.getStatusCode());
            }
        } catch (Exception e) {
            System.out.println("请求发生错误：" + e.getMessage());
        }
    }
}
